//
//  YWReportTroubleSubscriptionTableViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/5/10.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWReportTroubleSubscriptionTableViewController.h"
//#import "YWNoQrcodeSelectLocationTableViewController.h"
#import "YWReportTroubleSelectEquipmentSystemTypeTableViewController.h"
#import "YWSelectEquipmentSystemTableViewController.h"
#import "YWSelectBuildingApartmentTVC.h"
#import "YWMyPostTroublesViewController.h"

#import "YWMacro.h"
#import "YWConst.h"
#import "YWCommonTool.h"
#import "UIColor+CustomColors.h"
#import "YWLoginTool.h"
#import "YWLoginUser.h"
#import "MJRefresh.h"
#import "WMHttpTool.h"
#import "MJExtension.h"

#import "YWReportTroubleManager.h"
#import "YWBuilding.h"
#import "YWApartment.h"
#import "YWFloor.h"
#import "YWLocation.h"
#import "YWEquipmentManager.h"
#import "YWEquipmentType.h"
#import "YWBuildingManager.h"

#import "YWMeterReadingDetailRecordCell.h"
#import "YWSubscriptionTableViewCell.h"
#import "YWSubscripEquTypeTableViewCell.h"

@interface YWReportTroubleSubscriptionTableViewController ()<YWSubscriptionTableViewCellDelegate,YWReportTroubleSelectEquipmentSystemTypeTableViewControllerDelegate,YWSubscripEquTypeTableViewCellDelegate,YWSelectBuildingApartmentTVCDelegate>

//@property (nonatomic, strong) NSMutableArray * locationArray;
@property (nonatomic, strong) NSMutableArray * selLocArray;
@property (nonatomic, strong) NSMutableArray * systemTypeArray;
@property (nonatomic, strong) NSMutableArray * equipmentTypeArray;
@property (nonatomic, assign) NSInteger currentSelectTag;
@property (nonatomic, strong) NSMutableDictionary * equTypeDic;
@end

@implementation YWReportTroubleSubscriptionTableViewController

- (instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"subscription", nil),NSLocalizedString(@"postTrouble", nil)];//@"订阅报障";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"commit", nil) style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemEvent:)];//@"提交"
    [self loadLastTimeSubscriptionSelect];
    [self initNotification];
    [self initTableView];
}

- (void)loadLastTimeSubscriptionSelect{
    NSDictionary * saveDic = [NSKeyedUnarchiver unarchiveObjectWithFile:[self savefilePath:KYWRememberSelectSubscriptionKey]];
    self.selLocArray = [saveDic objectForKey:@"selLocArray"];
//    self.locationArray = [saveDic objectForKey:@"locationArray"];
    self.systemTypeArray = [saveDic objectForKey:@"systemTypeArray"];
    self.equipmentTypeArray = [saveDic objectForKey:@"equipmentTypeArray"];
    [self setupEquipmentTypeDictionary:self.equipmentTypeArray];
}

- (void)initNotification{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(popSelectedEquipmentTypeArray:) name:@"kselectEqumentTypeNoti" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(referenceTableview:) name:@"kCancelEquipmentTypeBtnEventNoti" object:nil];
}

- (void)initTableView{
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.separatorColor = [UIColor customLineColor];
    [self.tableView registerClass:[YWMeterReadingDetailRecordCell class] forCellReuseIdentifier:YWMeterReadingDetailRecordCellIdentifier];
    [self.tableView registerClass:[YWSubscriptionTableViewCell class] forCellReuseIdentifier:YWSubscriptionTableViewCellIdentifier];
    [self.tableView registerClass:[YWSubscripEquTypeTableViewCell class] forCellReuseIdentifier:YWSubscripEquTypeTableViewCellIdentifier];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTop)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)refreshTop{
    YWWeakSelf(weakSelf);
    [weakSelf.tableView.mj_footer resetNoMoreData];
    NSDictionary *headers = [YWCommonTool addSubHttpHeadWithDic:[WMHttpTool shareHttpTool].HTTPHeaders];
    [WMHttpTool shareHttpTool].HTTPHeaders = headers;
    if (headers == nil) {return;}
    [[WMHttpTool shareHttpTool] getWithUrl:[YWCommonTool requestSubscriptionReportTroubleUrl] params:nil success:^(NSHTTPURLResponse *response, id json) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSDictionary * dic = (NSDictionary*)json;
        NSArray * buildingArray = [NSArray array];
        buildingArray = [YWBuilding mj_objectArrayWithKeyValuesArray:[dic objectForKey:@"buildings"]];
        NSArray * apartmentArray = [NSArray array];
        apartmentArray = [YWApartment mj_objectArrayWithKeyValuesArray:[dic objectForKey:@"apartments"]];
        weakSelf.selLocArray = nil;
        [weakSelf.selLocArray addObjectsFromArray:buildingArray];
        [weakSelf.selLocArray addObjectsFromArray:apartmentArray];
        weakSelf.systemTypeArray = nil;
        weakSelf.systemTypeArray = [YWEquipmentSystemType mj_objectArrayWithKeyValuesArray:[dic objectForKey:@"systemTypes"]];
        //保存
        NSMutableDictionary * saveDic = [NSMutableDictionary dictionary];
        [saveDic setObject:self.selLocArray
                    forKey:@"selLocArray"];
        [saveDic setObject:self.systemTypeArray forKey:@"systemTypeArray"];
        NSDictionary * saveHaveselectInfo = [NSDictionary dictionaryWithDictionary:saveDic];
        [NSKeyedArchiver archiveRootObject:saveHaveselectInfo toFile:[self savefilePath:KYWRememberSelectSubscriptionKey]];
        
        [weakSelf.tableView reloadData];
    } failure:^(NSHTTPURLResponse *response, NSError *error) {
        YWLog(@"get subscription option failure");
        [YWCommonTool showTextHUDWithContent:NSLocalizedString(@"requestFailed", nil)];
    }];
}

/*- (void)referenceTableView:(NSNotification*)noti{
    YWEquipmentSystemType *equSysType = noti.userInfo[@"systemType"];
    if (equSysType) {
        if (self.equipmentTypeArray.count>0) {
            NSMutableArray *deleteArray = [NSMutableArray array];
            for (int i = 0;i<self.equipmentTypeArray.count;i++) {
                YWEquipmentType *equipmentType = self.equipmentTypeArray[i];
                if (equipmentType.equipmentSystemType.systemTypeId == equSysType.systemTypeId) {
                    [deleteArray addObject:equipmentType];
                }
            }
            [self.equipmentTypeArray removeObjectsInArray:deleteArray];
        }        
    }
    [self.tableView reloadData];
}*/

- (void)referenceTableview:(NSNotification*)noti{
    YWEquipmentType *equipmentType = noti.userInfo[@"equipmentType"];
    [self.equipmentTypeArray removeObject:equipmentType];
    [self.equTypeDic removeAllObjects];
    [self setupEquipmentTypeDictionary:self.equipmentTypeArray];
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.selLocArray.count>0 ? self.selLocArray.count : 1;
    }else{// if (section == 1)
        return 1;
    }
//    else{
//        return [[self.equTypeDic allKeys] count] + 1;
//    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        YWMeterReadingDetailRecordCell * cell = [tableView dequeueReusableCellWithIdentifier:YWMeterReadingDetailRecordCellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        if (self.selLocArray.count>0) {
            id object = self.selLocArray[indexPath.row];
            if ([object isKindOfClass:[YWBuilding class]]) {
                YWBuilding * building = (YWBuilding*)object;
                NSString * selLocationStr = [NSString stringWithFormat:@"%@",building.buildingName?:@""];
                cell.contentLabel.text = selLocationStr;
            }else if ([object isKindOfClass:[YWApartment class]]){
                YWApartment *apartment = (YWApartment*)object;
                YWApartment *searchApartment = [[YWBuildingManager shareManager] apartmentWithId:apartment.apartmentId];
                YWBuilding *building = [[YWBuildingManager shareManager] buildingWithId:searchApartment.buildingId];
                NSString * selLocationStr = [NSString stringWithFormat:@"%@%@",building.buildingName?:@"",apartment.apartmentName?:@""];
                cell.contentLabel.text = selLocationStr;
            }
            return cell;
        }else{
            cell.contentLabel.text = [NSString stringWithFormat:@"%@位置",NSLocalizedString(@"pleaseSelect", nil)];//@"请选择位置";
            return cell;
        }
    }else{// if (indexPath.section == 1)
        YWSubscriptionTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWSubscriptionTableViewCellIdentifier];
        cell.delegate = self;
        cell.btnTag = KSelectSystemTypeBtnTag;
        cell.systemTypeArray = self.systemTypeArray;
        return cell;
    }
//    else{
//        if (indexPath.row==0) {
//            YWSubscriptionTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWSubscriptionTableViewCellIdentifier];
//            cell.delegate = self;
//            cell.btnTag = KSelectEquipmentTypeBtnTag;
//            return cell;
//        }
//        YWSubscripEquTypeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWSubscripEquTypeTableViewCellIdentifier];
////        YWSubscriptionTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWSubscriptionTableViewCellIdentifier];
//        cell.delegate = self;
////        cell.btnTag = KSelectEquipmentTypeBtnTag;
//        NSString * key = [self.equTypeDic allKeys][indexPath.row-1];
//        cell.equipmentTypeArray = [self.equTypeDic objectForKey:key];
//        return cell;
//    }
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (indexPath.section == 0 && [self.selLocArray count]>0) ? UITableViewCellEditingStyleDelete : UITableViewCellEditingStyleNone;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0 && [self.selLocArray count]>0) {
        [self.selLocArray removeObjectAtIndex:indexPath.row];
        if (self.selLocArray.count==0) {
            [tableView reloadData];
        }else{
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        YWSelectBuildingApartmentTVC * selectLocationVC = [[YWSelectBuildingApartmentTVC alloc] init];
        selectLocationVC.selLocArray = [[NSMutableArray alloc] initWithArray:self.selLocArray];
        selectLocationVC.delegate = self;
        [self.navigationController pushViewController:selectLocationVC animated:YES];
    }
}
- (void)selectLocationArray:(NSMutableArray*)didselectArray{
    [self.selLocArray removeAllObjects];
    [self.selLocArray addObjectsFromArray:didselectArray];
//    [self.locationArray removeAllObjects];
//    for (id obj in didselectArray) {
//        if ([obj isKindOfClass:[YWBuilding class]]) {
//            YWBuilding * building = (YWBuilding*)obj;
//            NSString * selLocationStr = [NSString stringWithFormat:@"%@",building.buildingName?:@""];
//            [self.locationArray addObject:selLocationStr];
//        }else if ([obj isKindOfClass:[YWApartment class]]){
//            YWApartment *apartment = (YWApartment*)obj;
//            YWBuilding *building = [[YWBuildingManager shareManager] buildingWithId:apartment.buildingId];
//            NSString * selLocationStr = [NSString stringWithFormat:@"%@%@",building.buildingName?:@"",apartment.apartmentName?:@""];
//            [self.locationArray addObject:selLocationStr];
//        }
//        else if ([obj isKindOfClass:[YWFloor class]]){
//            YWFloor *floor = (YWFloor*)obj;
//            YWApartment *apartment = [[YWBuildingManager shareManager]apartmentWithId:floor.apartmentId];
//            YWBuilding *building = [[YWBuildingManager shareManager] buildingWithId:apartment.buildingId];
//            NSString * selLocationStr = [NSString stringWithFormat:@"%@%@%@",building.buildingName?:@"",apartment.apartmentName?:@"",floor.floorName?:@""];
//            [self.locationArray addObject:selLocationStr];
//        }else if ([obj isKindOfClass:[YWLocation class]]){
//            YWLocation *location = (YWLocation*)obj;
//            YWFloor *floor = [[YWBuildingManager shareManager] floorWithId:location.floorId];
//            YWApartment *apartment = [[YWBuildingManager shareManager]apartmentWithId:floor.apartmentId];
//            YWBuilding *building = [[YWBuildingManager shareManager] buildingWithId:apartment.buildingId];
//            NSString * selLocationStr = [NSString stringWithFormat:@"%@%@%@%@",building.buildingName?:@"",apartment.apartmentName?:@"",floor.floorName?:@"",location.locationName?:@""];
//            [self.locationArray addObject:selLocationStr];
//        }
//    }
    [self.tableView reloadData];
}
#pragma mark - delegate

- (void)jumpToSelectType:(NSInteger)buttonTag{
    _currentSelectTag = buttonTag;
    switch (_currentSelectTag) {
        case KSelectSystemTypeBtnTag:{//选择报障类别
        YWReportTroubleSelectEquipmentSystemTypeTableViewController * selectSystemTypeVC = [[YWReportTroubleSelectEquipmentSystemTypeTableViewController alloc] init];
            selectSystemTypeVC.delegate = self;
            selectSystemTypeVC.haveSelectTypeArr = self.systemTypeArray;
            [self.navigationController pushViewController:selectSystemTypeVC animated:YES];
        }
            break;
        case KSelectEquipmentTypeBtnTag:{//选择设备类型
            YWSelectEquipmentSystemTableViewController *selectEquSystemTVC = [[YWSelectEquipmentSystemTableViewController alloc] init];
            selectEquSystemTVC.equipmentTypeArray = self.equipmentTypeArray;
            selectEquSystemTVC.isFromSubscripVC = YES;
            [self.navigationController pushViewController:selectEquSystemTVC animated:YES];
        }
            break;
        default:
            break;
    }
}

- (void)cancelButtonEvent:(NSInteger)buttonTag withSystemType:(YWEquipmentSystemType*)systemType{
    [self.tableView reloadData];
}

- (void)cancelBtnEventWithEquType:(NSInteger)buttonTag{
    [self.tableView reloadData];
}

- (void)cancelBtnEventWithEquipmentType:(NSInteger)buttonTag withEquipmentType:(YWEquipmentType*)equType{
    NSDictionary * dic = [NSDictionary dictionaryWithObject:equType forKey:@"equipmentType"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kCancelEquipmentTypeBtnEventNoti" object:nil userInfo:dic];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 44;
    }else if (indexPath.section == 1){
        __block CGFloat w = 0;
        __block CGFloat h = 10;//距离父视图的H的间距
        [self.systemTypeArray enumerateObjectsUsingBlock:^(YWEquipmentSystemType*  _Nonnull systemType, NSUInteger idx, BOOL * _Nonnull stop) {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
            CGSize size =[systemType.systemTypeName sizeWithAttributes:@{NSFontAttributeName:kContentFont}];
            CGFloat length = size.width;
            button.frame = CGRectMake(kMarginW + w, h, length + 15 , 30);
            if(kMarginW + w + length + 15 + 10 > SCREEN_WIDTH){
                w = 0;
                h = h + button.frame.size.height + 10;
                button.frame = CGRectMake(kMarginW + w, h, length + 20, 30);
            }
            w = CGRectGetMaxX(button.frame);
        }];
        if (self.systemTypeArray.count>0) {
            if (kMarginW+w+KNameLabelWidth + 10  > SCREEN_WIDTH) {
                return h+(KNameLabelHeight+10)*2;
            }else{
                return h+KNameLabelHeight+10;
            }
        }else{
            return KNameLabelHeight+KcontentGap*2;
        }
    }else{
        if (indexPath.row==0) {
            return KNameLabelHeight+2*kMarginW;
        }else{
            NSString * key = [self.equTypeDic allKeys][indexPath.row-1];
            NSMutableArray *rowArray = [self.equTypeDic objectForKey:key];
            __block CGFloat w = 0;
            __block CGFloat h = 50;
            [rowArray enumerateObjectsUsingBlock:^(YWEquipmentType*  _Nonnull equipmentType, NSUInteger idx, BOOL * _Nonnull stop) {
                UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
                CGSize size =[equipmentType.equipmentTypeName sizeWithAttributes:@{NSFontAttributeName:kContentFont}];
                CGFloat length = size.width;
                button.frame = CGRectMake(10 + w, h, length + 15 , 30);
                if(10 + w + length + 15 + 10 > SCREEN_WIDTH){//最后的10是间隙
                    w = 0;
                    h = h + button.frame.size.height + 10;
                    button.frame = CGRectMake(10 + w, h, length + 20, 30);
                }
                w = CGRectGetMaxX(button.frame);
            }];
            if (rowArray.count>0) {
                return h+KNameLabelHeight;
            }else{
                return h;
            }
        }
    }
}
#pragma mark - table view section header view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    static NSString* HeaderIdentifier = @"YWReportTroubleSubscriptionTableViewControllerForHeaderInSection";
    UITableViewHeaderFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if (headerView == nil) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
        headerView.contentView.backgroundColor = [UIColor customBackgroudColor];
        UILabel* label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor customTextGrayColor];
        label.tag = 2016;
        label.font = KTitleFont;
        [headerView addSubview:label];
    }
    
    NSString *headText = [self titleForHeaderInSection:section];
    UILabel* label = [headerView viewWithTag:2016];
    label.text = headText;
    label.frame = CGRectMake(kMarginW, 0, SCREEN_WIDTH - 2 * kMarginW, 35);
    return headerView;
}

- (NSString *)titleForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return @"位置";
    }else if (section == 1){
        return [NSString stringWithFormat:@"%@%@",NSLocalizedString(@"postTrouble", nil),NSLocalizedString(@"category", nil)];//@"报障类别";
    }else{
        return NSLocalizedString(@"equipmentType", nil);//@"设备类型";
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}
#pragma mark - table view section footer view
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    static NSString* HeaderIdentifier = @"YWReportTroubleSubscriptionTableViewControllerForFooterInSection";
    UITableViewHeaderFooterView* footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if (footerView == nil) {
        footerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
        footerView.contentView.backgroundColor = [UIColor customBackgroudColor];
        UILabel* label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor customTextGrayColor];
        label.numberOfLines = 0;
        label.tag = 2017;
        label.font = KSmallContentFont;
        [footerView addSubview:label];
    }
    
    NSString *headText = [self titleForFooterInSection:section];
    UILabel* label = [footerView viewWithTag:2017];
    label.text = headText;
    if (section != 0) {
        label.frame = CGRectMake(kMarginW, 0, SCREEN_WIDTH - 2 * kMarginW, 60);
    }else{
        label.frame = CGRectMake(kMarginW, 0, SCREEN_WIDTH - 2 * kMarginW, 35);
    }
    return footerView;
}

- (NSString *)titleForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return [NSString stringWithFormat:@"位置指定您可接收到%@的位置",NSLocalizedString(@"postTrouble", nil)];//@"位置指定您可接收到报障的位置";
    }else if (section == 1){
        return NSLocalizedString(@"reportTroubleSubscriptionTroubleTypeTip", nil);//@"报障类型指定您可接收到的报障类型，其它类型的报障将不会推送";
    }else{
//        NSString * firstStr = [NSString stringWithFormat:@"%@指定您可接收到的%@的%@",NSLocalizedString(@"equipmentType", nil),NSLocalizedString(@"postTrouble", nil),NSLocalizedString(@"equipmentType", nil)];
        return @"设备类型指定您可接收到的报障的设备类型\n注：选择报障类别下属设备类型，您将只可接收到该设备类型的报障";
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return section != 0 ? 60 : 35;
}

- (void)popSelectedEquipmentSystemTypeArray:(NSArray *)systemTypeArray{
    [self.systemTypeArray removeAllObjects];
    [self.systemTypeArray addObjectsFromArray:systemTypeArray];
    [self.tableView reloadData];
}

- (void)popSelectedEquipmentTypeArray:(NSNotification*)noti{
    NSArray *equTypeArray = noti.userInfo[@"didSelEquTypeArray"];
    self.equipmentTypeArray = nil;
    [self.equipmentTypeArray addObjectsFromArray:equTypeArray];
    [self.equTypeDic removeAllObjects];
    [self setupEquipmentTypeDictionary:self.equipmentTypeArray];
    [self.tableView reloadData];
}

- (void)setupEquipmentTypeDictionary:(NSMutableArray*)equipmentTypeArray{
    __block NSMutableArray *tempArray = nil;
    [self.equipmentTypeArray enumerateObjectsUsingBlock:^(YWEquipmentType*  _Nonnull equipmentType, NSUInteger idx, BOOL * _Nonnull stop) {
        tempArray = [self.equTypeDic objectForKey:[NSString stringWithFormat:@"%@",@(equipmentType.equipmentSystemType.systemTypeId)]];
        if (tempArray == nil) {
            tempArray = [NSMutableArray array];
        }
        [tempArray addObject:equipmentType];
        if (![[self.equTypeDic allKeys] containsObject:@(equipmentType.equipmentSystemType.systemTypeId)]) {
            [self.equTypeDic setObject:tempArray forKey:[NSString stringWithFormat:@"%@",@(equipmentType.equipmentSystemType.systemTypeId)]];        }
    }];
}

- (void)rightBarButtonItemEvent:(id)sender{
//    [saveDic setObject:self.equipmentTypeArray forKey:@"equipmentTypeArray"];
    [[YWReportTroubleManager shareManager] postSubscriptionOptionsWithlocationArray:self.selLocArray systemArray:self.systemTypeArray WithComplete:^(BOOL success) {
        if (success) {
            NSMutableDictionary * saveDic = [NSMutableDictionary dictionary];
            [saveDic setObject:self.selLocArray
                        forKey:@"selLocArray"];
//            [saveDic setObject:self.locationArray forKey:@"locationArray"];
            [saveDic setObject:self.systemTypeArray forKey:@"systemTypeArray"];
            NSDictionary * saveHaveselectInfo = [NSDictionary dictionaryWithDictionary:saveDic];
            [NSKeyedArchiver archiveRootObject:saveHaveselectInfo toFile:[self savefilePath:KYWRememberSelectSubscriptionKey]];
            UIViewController *vc = nil;
            for (UIViewController *temp in self.navigationController.viewControllers) {
                if ([temp isKindOfClass:[YWMyPostTroublesViewController class]]) {
                    vc = temp;
                    break;
                }
            }
            if (vc) {
                [self.navigationController popToViewController:vc animated:YES];
            }
          [YWCommonTool showTextHUDWithContent:[NSString stringWithFormat:@"%@成功",NSLocalizedString(@"subscription", nil)]];//@"订阅成功"
        }
    }];
    //提交,（设备类型优先于报障类别，即在该报障类别下选择了设备类型，那么不再提交报障类别Id只提交设备类型Id）
    /*
    NSMutableArray * postSystemTypeArray = [NSMutableArray array];
    [self.systemTypeArray enumerateObjectsUsingBlock:^(YWEquipmentSystemType *  _Nonnull equSystemType, NSUInteger idx, BOOL * _Nonnull stop) {
        if (![[self.equTypeDic allKeys] containsObject:@(equSystemType.systemTypeId)]) {
            [postSystemTypeArray addObject:equSystemType];
        }
    }];*/
    //这样，上传的就是没有选择设备类型的报障类别postSystemTypeArray
    
}

- (NSString*)savefilePath:(NSString*)fileName
{
    NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:fileName];
    return filePath;
}

#pragma mark 懒加载
//- (NSMutableArray*)locationArray{
//    if (!_locationArray) {
//        _locationArray = [NSMutableArray array];
//    }
//    return _locationArray;
//}

- (NSMutableArray*)selLocArray{
    if (!_selLocArray) {
        _selLocArray = [NSMutableArray array];
    }
    return _selLocArray;
}

- (NSMutableArray*)systemTypeArray{
    if (!_systemTypeArray) {
        _systemTypeArray = [NSMutableArray array];
    }
    return _systemTypeArray;
}

- (NSMutableArray*)equipmentTypeArray{
    if (!_equipmentTypeArray) {
        _equipmentTypeArray = [NSMutableArray array];
    }
    return _equipmentTypeArray;
}

- (NSMutableDictionary*)equTypeDic{
    if (!_equTypeDic) {
        _equTypeDic = [NSMutableDictionary dictionary];
    }
    return _equTypeDic;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
